package gr.atc.epart;

import gr.atc.epart.googlewrapper.GoogleServiceCallbackType;
import gr.atc.epart.googlewrapper.moderator.GoogleModerator;
import gr.atc.epart.googlewrapper.moderator.ModeratorSeries;
import gr.atc.epart.googlewrapper.moderator.ModeratorTopic;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DeliberationAddTopicActivity extends Activity
{
	private GoogleModerator _moderator;
	private ModeratorSeries _series;

	private TextView   _topicTitle;
	private TextView   _topicDescription;
	private ProgressDialog _progressDialog;

	//
	// used to receive callbacks   
	//
	 private Handler _messageHandler = new Handler() 
	  {
	      public void handleMessage(Message msg) 
	      {  
	          switch(msg.what) 
	          {
	        	  case GoogleServiceCallbackType.ModeratorNewTopicAdded:
	        		  
	        		    dismissProgessDialog();
	        		  	showTopicAddedMessage();

	        		  	setResult(Activity.RESULT_OK);
	        		  	finish();

	        		  // TODO:: this activity is called for result, we can pass back the
	        		  // new submission, and update the list locally...

	        		  // this is not as easy as it sounds...the objects should be parcelable...
	        		  // although everywhere else I use tha whole array which IS serialisable and it works...

	        		  	/*
        			    ArrayList<ModeratorSubmission> submision = (ArrayList<ModeratorSubmission>) msg.getData().getSerializable("SUBMISSION");

        			    Intent returnData = new Intent();
        			    returnData.putExtra("gr.atc.epart.SUBMISSION", (Serializable)submision);
	        		  	
	        		  	setResult(Activity.RESULT_OK,returnData);
	        		  	finish();
	        		  	*/
	        		  	

	        		  break;

	        	  case GoogleServiceCallbackType.OperationError:
	        	  	{
	        	  		if (msg.getData()!=null)
	        	  		{
		        	  		String error = msg.getData().getString("DESCRIPTION");
		        	  		showErrorAlert("Google moderator error:"+error);
		        	  	
	        	  		}
	        	  		dismissProgessDialog();
	        	  	}
	        		  break;
	          } 
	      }
	  };

	@Override
	public void onCreate(Bundle icicle) 
	{
		
		super.onCreate(icicle);
		setContentView(R.layout.addtopic);
		
		//
		// there is an serious issue with moderator. It still cannot work with the AccountManager's tokens
		// and authenticationi s performed through ClientLogin
		// this means that we cannot neither create multiple instances (beacuse each one needs to go through the authentication phase 
		// to retrieve the token) nor re-use the same instance because the event handler belongs to another activity.
		//
		// a work around is to create new instances ans share the token ASSUMING that it is still valid
		//
		_moderator = new GoogleModerator(_messageHandler);
		_moderator.setAuthToken(EPartVolatileDataStore.getInstance().getModeratorAuthToken());
		
		_series = EPartVolatileDataStore.getInstance().getModeratorSeries();
		
		_topicTitle = (TextView) findViewById(R.id.addtopictitle);
		_topicDescription= (TextView) findViewById(R.id.addtopictext);
		
		 OnClickListener addTopicButtonListener = new OnClickListener() 
		   {
			  	public void onClick(View v) 
			  	{
			  		showProgressDialog();
			  		String username = AccountUtil.getAccountName(DeliberationAddTopicActivity.this.getBaseContext());
			  		_moderator.addTopic(_series, _topicTitle.getText().toString(), _topicDescription.getText().toString(), username);
			  	}
		   };
		   
		Button addTopicButton = (Button)findViewById(R.id.addtopicbutton);
		addTopicButton.setOnClickListener(addTopicButtonListener);	
		
	}
	

	private void showTopicAddedMessage() 
	{
		Toast.makeText(getBaseContext(), "Your topic was submitted!", Toast.LENGTH_LONG).show();
	}

	private void showProgressDialog() {
		
		_progressDialog = new ProgressDialog(this);
		_progressDialog.setMessage("Please wait...");
		_progressDialog.setIndeterminate(true);
		_progressDialog.setCancelable(false);
		_progressDialog.show();
	}

	private void dismissProgessDialog()
	{
		_progressDialogHandler.sendEmptyMessage(0);
	}
	
	   // Define the Handler that receives messages from the thread and update the progress
	final Handler _progressDialogHandler = new Handler() 
	{
		public void handleMessage(Message msg) 
		{
			if (_progressDialog!=null)
				_progressDialog.dismiss();

		}
	};

	public void showErrorAlert(String msg) {

		AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
		dlgAlert.setMessage(msg);
		dlgAlert.setTitle("Error");
		dlgAlert.setPositiveButton("OK", null);
		dlgAlert.setCancelable(true);
		dlgAlert.create();
		dlgAlert.show();
	}

}
